/**
 * 采用顺序存储结构实现队列
 * 队列是操作受限的线性结构，尾部插入，头部删除
 * 因此具有 先进先出，后进后出的特性
 */

 class QueueSequence {
     constructor (length) {
         this.head = -1
         this.rear = -1
         this.data = new Array(length)
         this.count = 0
     }
     /**
      * 尾部入队操作
      */
     push (val) {
        let status = 'overflow'
        if (!this.isFull()) {
            this.rear = this.rear + 1 % this.data.length
            this.data[this.rear] = val
            this.count++
            status = 'success'
        }
        return status
     }
     /**
      * 头部出队操作
      */
     shift () {
        let status = 'underflow'
        if (!this.isEmpty()) {
            this.data[this.head + 1] = undefined
            this.head = this.head + 1 % this.data.length
            this.count--
            status = 'success'
        }
        return status
     }
     /**
      * 判断是否满了
      */
     isFull () {
        return this.count === this.data.length
     }  
     /**
      * 判断是否为空
      */
     isEmpty () {
        return this.count === 0
     }
     /**
      * 返回队列元素个数
      */
     size () {
        return this.count
     }
     /**
      * 清空队列
      */
     clear () {
         this.head = -1
         this.rear = -1
     }
     /**
      * 销毁队列
      */
     destory () {
        this.data = null
     }
 }

 const a = new QueueSequence(10)
 a.push('d')
 a.push('t')
 a.push('x')
 a.push('d')
 a.push('t')
 a.push('x')
 a.push('d')
 a.push('t')
 a.push('x')
 a.push(3)
 a.push(2)
 a.push(1)
 console.log(a.data)
 a.shift()
 a.shift()
 console.log(a.data)
 console.log(a.size())
